package com.coupon.service.impl;

import com.coupon.mapper.ManagerMapper;
import com.coupon.pojo.Manager;
import com.coupon.service.ManagerService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author :wn
 * @program : coupon
 * @descript :
 * @create :2021-01-07 09:03
 */
@Service("managerService")
public class ManagerServiceImpl implements ManagerService {
    @Resource
    ManagerMapper managerMapper;

    /**登录业务方法*/
    @Override
    public Manager login(String phone) {
        Manager manager=managerMapper.selectByPhone(phone);
        return manager;
    }

    /**注册业务方法*/
    @Override
    public Boolean registerManage(Manager manager) {
        //注册时，判断工作人员输入的手机号是否已经被注册过.
        Manager selectManage= managerMapper.selectByPhone(manager.getPhone());
        Integer insert=0;
        //如果没有被注册，则继续注册流程
        if (selectManage==null)
        {
            insert=managerMapper.insertManage(manager);
        }

        //否则注册失败
        return insert>0;
    }

    /**修改业务方法*/
    @Override
    public Boolean updateManage(Manager manager) {
        Manager selectManage= managerMapper.selectByPhone(manager.getPhone());
        Integer update=0;
        //如果用户存在，则继续修改
        if (selectManage!=null)
        {
            update=managerMapper.updateManage(manager);
        }
        return update>0;
    }

    /**删除业务方法*/
    @Override
    public Boolean deleteManage(String phone) {
        //删除时，判断用户是否存在
        Manager selectManage= managerMapper.selectByPhone(phone);
        Integer delete=0;
        //如果用户存在，则继续删除
        if (selectManage!=null)
        {
            delete=managerMapper.deleteManage(phone);
        }
        return delete>0;
    }

    /**分页获取所有工作人员信息方法*/
    @Override
    public Map findAll(Integer page,String name) {
        Map dataMap = new HashMap();
        if (page==null&&name==null)
        {
            List<Manager> managers = managerMapper.selectAllNoArgs();
            dataMap.put("data", managers);
        }
        else {
            //在业务层对页面做转化，转化为limit查询中的offset的偏移量
            List<Manager> managers = managerMapper.selectAllManage((page - 1) * 10, "%" + name + "%");
            Integer count = managerMapper.countAll();
            dataMap.put("data", managers);
            dataMap.put("count", count);
        }

        return dataMap;
    }
    /**统计所有工作人员数据方法*/
    @Override
    public Integer countAll() {
        return managerMapper.countAll();
    }
}
